/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package trackreggui;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.ComboBoxModel;
import javax.swing.ListModel;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import webreg.model.Athlet;
import webreg.model.container.AgeTableContainer;
import webreg.model.container.AthletContainer;
import webreg.model.container.DisciplinContainer;
import webreg.model.swing.AgeTableContainerComboBoxModel;
import webreg.model.swing.AthletContainerListModel;
import webreg.model.swing.DisciplinContainerListModel;

/**
 *
 * @author STKNOLL
 */
public class DataPool {
    private AthletContainer mAthletPool;
    private DisciplinContainer mDisciplinPool;
    private AgeTableContainer mAgeTablePool;
    private static DataPool mInstance = null;

    private DataPool() {
        mAthletPool = null;
        mDisciplinPool = null;
        mAgeTablePool = null;
        loadAthletPool();
        loadDisciplinPool();
        loadAgeTablePool();
    }

    private DataPool (DataPool toCopy) {
    }

    public static DataPool getInstance() {
        if (mInstance == null) {
            mInstance = new DataPool();
        }
        return mInstance;
    }

    public void saveAllPoolData() {
        saveAthletPool();
        saveDisciplinPool();
        saveAgeTablePool();
    }

    /** Load all athlets from a file
     */
    private void loadAthletPool() {
        JAXBContext context = null;
        try
		{
			InputStream fis = null;
			fis = new FileInputStream( Configurator.getInstance().getAthletPoolFile() );
			context = JAXBContext.newInstance( AthletContainer.class );
			Unmarshaller um = context.createUnmarshaller();
			mAthletPool = (AthletContainer) um.unmarshal(fis);
			fis.close();
		} catch (JAXBException e1) {
			e1.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

    private void saveAthletPool() {
		JAXBContext context = null;
		try {
			FileOutputStream fos = new FileOutputStream( Configurator.getInstance().getAthletPoolFile() );
			context = JAXBContext.newInstance( AthletContainer.class );
			Marshaller m = context.createMarshaller(); 
			m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE ); 
			m.marshal( mAthletPool, fos );
			fos.close();			
		} catch (JAXBException e1) {
			e1.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
    }

    /** Load all disciplins from a file
     */
    private void loadDisciplinPool() {
        JAXBContext context = null;
        try
		{
			InputStream fis = null;
			fis = new FileInputStream( Configurator.getInstance().getDisciplinPoolFile() );
			context = JAXBContext.newInstance( DisciplinContainer.class );
			Unmarshaller um = context.createUnmarshaller();
			mDisciplinPool = (DisciplinContainer) um.unmarshal(fis);
			fis.close();
		} catch (JAXBException e1) {
			e1.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

    private void saveDisciplinPool() {
		JAXBContext context = null;
		try {
			FileOutputStream fos = new FileOutputStream( Configurator.getInstance().getDisciplinPoolFile() );
			context = JAXBContext.newInstance( DisciplinContainer.class );
			Marshaller m = context.createMarshaller();
			m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
			m.marshal( mDisciplinPool, fos );
			fos.close();
		} catch (JAXBException e1) {
			e1.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

    /** Load all age tables from a file
     */
    private void loadAgeTablePool() {
        JAXBContext context = null;
        try
		{
			InputStream fis = new FileInputStream( Configurator.getInstance().getAgeTablePoolFile() );
			context = JAXBContext.newInstance( AgeTableContainer.class );
			Unmarshaller um = context.createUnmarshaller();
			mAgeTablePool = (AgeTableContainer) um.unmarshal(fis);
			fis.close();
		} catch (JAXBException e1) {
			e1.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

    private void saveAgeTablePool() {
		JAXBContext context = null;
		try {
			FileOutputStream fos = new FileOutputStream( Configurator.getInstance().getAgeTablePoolFile()  );
			context = JAXBContext.newInstance( AgeTableContainer.class );
			Marshaller m = context.createMarshaller();
			m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
			m.marshal( mAgeTablePool, fos );
			fos.close();
            System.out.println("Saved age table pool");
		} catch (JAXBException e1) {
			e1.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}	
    }



    public Athlet getAthletFromAthletPool(int index) {
        return (Athlet) mAthletPool.getAthlets().toArray()[index];
    }
    
    public ListModel getAthletPoolAsListModel() {
        AthletContainerListModel lm = new AthletContainerListModel(mAthletPool.getAthlets() );
        return lm;
    }

    public ListModel getDisciplinPoolAsListModel() {
        DisciplinContainerListModel lm = new DisciplinContainerListModel(mDisciplinPool.getDisciplins() );
        return lm;
    }

    public ComboBoxModel getAgeTablePoolAsComboBoxModel() {
        AgeTableContainerComboBoxModel cbm = new AgeTableContainerComboBoxModel( mAgeTablePool.getAgeTables() );
        return cbm;
    }
}
